<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!--通用查询-->
      <queryHeader :dict="dict" :permission="permission" />
      <!--通用编辑-->
      <crudOperation :permission="permission" />
    </div>

    <!--表格渲染-->
    <el-table ref="table" v-loading="crud.loading" :data="crud.data" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
      <!--多选框-->
      <el-table-column type="selection" width="55" />
      <!--展示字段-->
      <el-table-column v-if="columns.visible('userIcon')" prop="userIcon" label="用户头像" :show-overflow-tooltip="true" />
      <el-table-column v-if="columns.visible('userName')" prop="userName" label="用户名" :show-overflow-tooltip="true" />
      <el-table-column v-if="columns.visible('userPhone')" prop="userPhone" label="用户手机号" :show-overflow-tooltip="true" />
      <el-table-column v-if="columns.visible('userEmail')" prop="userEmail" label="用户邮箱" :show-overflow-tooltip="true" />
      <el-table-column v-if="columns.visible('userActiveStatus')" prop="userActiveStatus" label="用户激活状态" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{ dict.label.user_active_status[scope.row.userActiveStatus] }}
        </template>
      </el-table-column>
      <el-table-column v-if="columns.visible('userStatus')" prop="userStatus" label="用户状态" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{ dict.label.user_auth_status[scope.row.userStatus] }}
        </template>
      </el-table-column>
      <el-table-column v-if="columns.visible('userStatusRemark')" prop="userStatusRemark" label="用户状态备注" :show-overflow-tooltip="true" />
      <el-table-column v-if="columns.visible('userRegisterTime')" prop="userRegisterTime" label="用户注册时间" :show-overflow-tooltip="true" />
      <el-table-column v-if="columns.visible('userRegisterChannel')" prop="userRegisterChannel" label="用户添加渠道" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{ dict.label.user_channel[scope.row.userRegisterChannel] }}
        </template>
      </el-table-column>
      <el-table-column v-if="columns.visible('userLastLoginTime')" prop="userLastLoginTime" label="用户上次登陆时间" :show-overflow-tooltip="true" />
      <el-table-column v-if="columns.visible('userLastLoginIp')" prop="userLastLoginIp" label="用户上次登陆IP" :show-overflow-tooltip="true" />

      <!--操作-->
      <el-table-column label="操作" width="280px" align="center" fixed="right">
        <template slot-scope="scope">
          <!--通用编辑删除-->
          <udOperation :data="scope.row" :permission="permission">
            <template slot="left">
              <el-button size="mini" type="text" @click="optThrid(scope.row)">三方账号</el-button>
              <el-button size="mini" type="text" @click="handleBindRoles(scope.row)">角色绑定</el-button>
            </template>
          </udOperation>
        </template>
      </el-table-column>
    </el-table>

    <!--分页组件-->
    <pagination />

    <!--表单渲染-->
    <editForm :dict="dict" />

    <el-dialog title="三方账户" :visible.sync="optThirdShow">
      <third-user :user-id="optRowUserId" />
    </el-dialog>

    <el-dialog title="角色绑定" :visible.sync="roleDialogVisiable">

      <el-checkbox-group v-model="roleItem.roles" v-loading="roleLoading">
        <el-checkbox v-for="role in roleList" :key="role.id" :label="role.id">{{ role.name }}</el-checkbox>
      </el-checkbox-group>

      <div slot="footer" class="dialog-footer">
        <el-button @click="roleDialogVisiable = false">
          取消
        </el-button>
        <el-button type="primary" @click="bindRoles()">
          提交
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import crudPlatformUser from '@/api/system/user/user'
import queryHeader from './module/queryHeader'
import editForm from './module/editForm'
import CRUD, { presenter } from '@crud/crud'
import crudOperation from '@crud/CRUD.operation'
import pagination from '@crud/Pagination'
import udOperation from '@crud/UD.operation'
import thirdUser from '@/views/system/platform/user/module/third/index'
import { getRoles, getUserRoles, userBindRoles } from '@/api/system/auth/role/role'

// crud交由presenter持有
const crud = CRUD({ title: 'PlatformUser', url: 'platform/user/list/page', crudMethod: { ...crudPlatformUser }, pkIdName: 'userId' })

export default {
  name: 'PlatformUser',
  components: { queryHeader, editForm, crudOperation, pagination, udOperation, thirdUser },
  dicts: ['user_active_status', 'user_auth_status', 'user_channel'],
  mixins: [presenter(crud)],
  data() {
    return {
      optRowUserId: null,
      optThirdShow: false,
      // 角色操作相关
      roleList: [], roleDialogVisiable: false, roleLoading: false, roleItem: { roles: ['2'], userId: undefined },
      // 对应几个按钮操作
      permission: {
        add: [],
        edit: [],
        del: []
      }
    }
  },
  created() {
    this.getRolesList()
  },
  methods: {
    optThrid(row) {
      this.optRowUserId = row.userId
      this.optThirdShow = true
    },
    getRolesList() {
      getRoles().then(data => {
        this.roleList = data
      })
    },
    handleBindRoles(row) {
      this.roleDialogVisiable = true
      this.roleItem.userId = row.userId
      this.roleLoading = true
      getUserRoles(row.userId).then((data) => {
        this.roleItem.roles = []
        data.forEach((value) => {
          this.roleItem.roles.push(value.id)
        })
        this.roleLoading = false
      }).catch(() => {
        this.roleLoading = false
      })
    },
    bindRoles() {
      this.roleLoading = true
      userBindRoles(this.roleItem.userId, this.roleItem.roles).then(() => {
        this.roleDialogVisiable = false
        this.roleLoading = false
      }).catch(() => {
        this.roleLoading = false
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  /deep/ .el-input-number .el-input__inner {
    text-align: left;
  }
</style>
